package cn.edu.zjicm.wordsnet_d.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.WordDetailActivity;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.TypefaceUtil;

public class WordDetailForHuanboFragment extends Fragment {

	public WordDetailForHuanboFragment(OneWord word) {
		this.mOneWord = word;
	}

	public WordDetailForHuanboFragment() {

	}

	private static final String BUNDLE_KEY_WORD_DETAIL = "bundle_key_word_detail";
	// Data
	private OneWord mOneWord;

	// View
	private ViewFlipper mWriteFlipper;
	private LinearLayout mFakeScrollView;
	private TextView mWordText;
	private TextView mWordPhonetic;
	private TextView mWordCn;
	private TextView mWordAff;
	private TextView cetTest;
	private LinearLayout mRelationshipContainer;
	// 右上角按钮
	private ImageView mReadButton;
	private TextView mWriteButton;
	// popup window
	private View v;
	private int buttonDrawable[];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		v = inflater.inflate(R.layout.fragment_word_detail_for_huanbo, null);

		mWriteFlipper = (ViewFlipper) v.findViewById(R.id.write_flipper);
		mFakeScrollView = (LinearLayout) v.findViewById(R.id.fake_scroll_view);
		mWordText = (TextView) v.findViewById(R.id.word_detail_word);

		mWordPhonetic = (TextView) v.findViewById(R.id.word_detail_phonetic);
		TypefaceUtil.getInstance(getActivity()).setTypeface(mWordPhonetic);
		mWordCn = (TextView) v.findViewById(R.id.word_detail_cn);

		mRelationshipContainer = (LinearLayout) v.findViewById(R.id.word_detail_relation_ship_item_container);
		mReadButton = (ImageView) v.findViewById(R.id.read_button);

		mWriteButton = (TextView) v.findViewById(R.id.write_button);
		cetTest = (TextView) v.findViewById(R.id.cet_test);

		mWordAff = (TextView) v.findViewById(R.id.word_aff);
		mWriteButton.setBackgroundDrawable(null);
		mWriteButton.setVisibility(View.GONE);
		removeReadIcon();

		if (mOneWord != null)
			initView();
		buttonDrawable = new int[2];
		if (NightModeUtil.isNightModeNow()) {
			buttonDrawable[0] = R.drawable.rectangle_green_button;
			buttonDrawable[1] = R.drawable.rectangle_gray_button;
		} else {
			buttonDrawable[0] = R.drawable.green_button_unclicked;
			buttonDrawable[1] = R.drawable.gray_button_unclicked;
		}
		return v;
	}

	public void initViewForNewWord(OneWord word) {
		this.mOneWord = word;
		initView();
	}

	private void initView() {
		if (mOneWord == null) {
			MobclickAgentData.exception(getActivity(), "in WordDetailForHuanboFragment initView(): mOneWord is null");
			return;
		}

		mWordText.setText(mOneWord.getLemma());
		mWordPhonetic.setText(mOneWord.getPhonetic());
		mWordCn.setText(mOneWord.getSensesSenior());

		// if (FileManager.isVoiceFileExist(getActivity(), mOneWord,true)) {
		// showReadIcon();
		// } else {
		// removeReadIcon();
		// }

		reLoadRelationShipList();

		if (mOneWord.getAff_content() != null && mOneWord.getAff_content().length() > 0) {
			mWordAff.setText("[记]" + mOneWord.getAff_content());
			mWordAff.setVisibility(View.VISIBLE);
		} else {
			mWordAff.setVisibility(View.GONE);
		}

		cetTest.setText(mOneWord.getBookName());
	}

	public void reLoadRelationShipList() {
		CreatView.reLoadRelationShip(mRelationshipContainer, mOneWord, getActivity());
	}

	private void showReadIcon() {
		mReadButton.setVisibility(View.VISIBLE);
	}

	// 隐藏发音图标
	void removeReadIcon() {
		mReadButton.setVisibility(View.GONE);
	}

	public void displayDetailView() {
		if (getActivity() instanceof WordDetailActivity) {
			((WordDetailActivity) getActivity()).setTitleText(mOneWord.getLemma());
		}
		mWriteFlipper.setDisplayedChild(0);
	}

}